<?php
namespace backend\controllers;

use Yii;
use common\models\Ad\AdPosition;
use common\models\Ad\AdPositionSearch;
use common\models\Ad\Ad;
use common\models\Ad\AdSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
use kartik\file\FileInput;//图片上传
use zhongkefu\zhongkefu;
use common\models\adminlog\AdminLog;
use zhongkefu\helpers\Utils;
/**
 * AdController implements the CRUD actions for Ad model.
 */
class AdController extends BackendController
{


    /**
     * Lists all Ad models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new AdSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Ad model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
		$model = $this->findModel($id);
		Adminlog::addlog(yii::$app->user->id, '查看广告 '.$model->ad_name);
        return $this->render('view', [
            'model' => $model,
			
        ]);
    }

    /**
     * Creates a new Ad model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
	 
	 
	public function actionCreate()
    {
        $model = new Ad();
        //上传图片插件
        if ($model->load(Yii::$app->request->post()) ) {
			$adname = Yii::$app->request->post()['Ad']['ad_name'];
            $model->ad_code = UploadedFile::getInstance($model, 'ad_code');
            if($model->ad_code){           
                $siteRoot = Yii::$app->params['adimg'];
                $json = Utils::uploadfile($_FILES['Ad']['tmp_name']['ad_code'],$_FILES['Ad']['name']['ad_code'],$siteRoot);
                $model->ad_code = $json;   		
            }
            if($model->save()){
				Adminlog::addlog(yii::$app->user->id, '添加广告 '.$adname);
                return $this->redirect(['view', 'id' => $model->ad_id]);
            }else{
                Yii::$app->session->setFlash('warning', Yii::t('app', '图片不能为空'));
                return $this->render('create', [
                    'model' => $model,
                ]);
            }
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
		
    }

    /**
     * Updates an existing Ad model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
	
      $model = $this->findModel($id);
        //上传图片插件
        if ($model->load(Yii::$app->request->post()) ) {
            $model->ad_code = UploadedFile::getInstance($model, 'ad_code');
            if($model->ad_code){
                $siteRoot = Yii::$app->params['adimg'];
                $json = Utils::uploadfile($_FILES['Ad']['tmp_name']['ad_code'],$_FILES['Ad']['name']['ad_code'],$siteRoot);
                $model->ad_code = $json;                
            }
			 if (!$model->ad_code) {
                $new = $this->findModel($id);
                $model->ad_code = $new->ad_code;
            }
            if($model->save()){
				$adname = Yii::$app->request->post()['Ad']['ad_name'];
				Adminlog::addlog(yii::$app->user->id, '修改广告 '.$adname);
                return $this->redirect(['view', 'id' => $model->ad_id]);
            }else{
                return $this->render('update', [
                    'model' => $model,
                ]);
            }
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing Ad model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $model = $this->findModel($id);
		$model->delete();
		Adminlog::addlog(yii::$app->user->id, '删除广告 '.$model->ad_name);
        return $this->redirect(['index']);
    }

    /**
     * Finds the Ad model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Ad the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Ad::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
	  /**
     * 删除按钮
     * If update is successful, the browser will be redirected to the 'index' page.
     * @return mixed
     */
    public function actionAlldelete()
    {
		$artname="";
        if(isset(zhongkefu::$app->request->post()["checkboxid"])){
            $ids = zhongkefu::$app->request->post()["checkboxid"];
            $ids = explode(',',$ids);
			foreach($ids as $val){
				$model = Ad::findOne(['ad_id' => $val]);
				$artname .= $model->ad_name.",";
			}
			$artname = substr($artname,0,-1);
            foreach($ids as $ad_id){
                $model = Ad::findOne(['ad_id' => $ad_id]);
                $this->findModel($ad_id)->delete();
            }
			Adminlog::addlog(yii::$app->user->id, "批量删除广告 ".$artname);
            return true;
        }
    }


       
}
